from uuid import uuid1
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy

from app import app

db = SQLAlchemy(app)


class Message(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    nickname = db.Column(db.Unicode(32))
    message = db.Column(db.Unicode(512))
    submit_time = db.Column(db.DateTime(timezone=False))
    uuid = db.Column(db.Unicode(32))

    def __init__(self, nickname, message, submit_time=None):
        self.nickname = nickname
        self.message = message
        if submit_time is None:
            submit_time = datetime.utcnow()
        self.submit_time = submit_time
        self.uuid = uuid1().hex

    def __repr__(self):
        return "<USER> %r" % self.nickname
